<template>
  <div class="main_box">
    <el-row class="header-row">
      <el-col>
        <div class="search_wrap">
          <el-form :inline="true" :model="filters" class="demo-form-inline" ref="searchForm">
            <!-- <HospitalSelect @hospitalId="getHospitalId" :site="2" :id="String(filters.hospitalId)"></HospitalSelect>
            <OfficeSelect @officeId="getOfficeId" :site="2" :hospital-id="Number(filters.hospitalId)" ></OfficeSelect> -->
            <el-form-item prop="beeperName">
              <el-input v-model="filters.beeperName" clearable placeholder="呼叫器名称"></el-input>
            </el-form-item>
            <el-form-item prop="beeperCode">
              <el-input v-model="filters.beeperCode" clearable placeholder="呼叫器编号"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button icon="el-icon-search" type="primary" @click="onSearch">查询</el-button>
              <el-button icon="el-icon-refresh-left" @click="resetForm">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-col>
      <el-col>
        <div class="opration_wrap">
          <el-tooltip content="刷新" placement="top">
            <el-button circle icon="el-icon-refresh-right" @click="reload" size="mini"></el-button>
          </el-tooltip>
          <div>
            <el-button type="primary" plain icon="fa fa-plus" v-has="permissionAdd" @click="handleClickAdd()" size="mini"> 新增</el-button>
            <el-button type="info" plain size="mini" icon="el-icon-user-solid" @click="handleHousehold()"> 住户管理</el-button>
            <el-button type="info" plain size="mini" icon="el-icon-s-shop" @click="handleBusiness()"> 商户管理</el-button>
          </div>
        </div>
      </el-col>
    </el-row>

    <el-row ref="tableRow">
      <el-table :data="dataList" :fit="true" highlight-current-row v-loading="listLoading" element-loading-text="数据加载中，请稍后..." empty-text="对不起，没有找到相关数据">
        <el-table-column type="index" label="序号" width="50" align="center"></el-table-column>
        <el-table-column prop="beeperName" label="呼叫器名称" width="150" align="center"></el-table-column>
        <el-table-column prop="beeperCode" label="呼叫器编号" width="150" align="center"></el-table-column>
        <!-- <el-table-column prop="wifiName" label="wifi账号" width="150"></el-table-column>
        <el-table-column prop="wifiPwd" label="wifi密码" width="150"></el-table-column> -->
        <el-table-column width="100" label="联系人" align="center" prop="linkName"> </el-table-column>
        <el-table-column width="200" label="联系电话" align="center" prop="linkPhone"> </el-table-column>
        <el-table-column align="center" label="联系地址" prop="address" width="220px"> </el-table-column>
        <el-table-column align="center" label="最新服务" prop="serverName" width="110px"> </el-table-column>
        <el-table-column prop="recordState" :formatter="formatRecordState" label="服务状态" align="center" width="80"> </el-table-column>
        <el-table-column align="center" label="服务时间" prop="serverDate" width="160px"> </el-table-column>
        <el-table-column align="center" label="商家" prop="businessName" width="140px"> </el-table-column>
        <el-table-column label="呼叫器状态" lign="center" width="100" align="center">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatBeeperStates(scope.row.beeperState)">
                {{ formatBeeperState(scope.row.beeperState) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" width="100">
          <template slot-scope="scope">
            <span>
              <el-tag size="mini" effect="dark" :type="formatStates(scope.row.state)">
                {{ formatState(scope.row.state) }}
              </el-tag>
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" width="200" align="center"></el-table-column>
        <el-table-column label="操作" align="center" fixed="right" width="200px">
          <template slot-scope="scope">
            <el-button v-has="permissionBeeper" @click.native="handleBeeperRecord(scope.row)" size="mini">呼叫记录</el-button>
            <el-button v-has="permissionCheck" @click="handleClickDetail(scope.row)" size="mini">查看</el-button>
            <el-dropdown trigger="click" size="small">
              <el-button type="primary"> <i class="fa fa-list-ul"></i>更多 </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item v-has="permissionEdit" @click.native="handleClickEditor(scope.row)">编辑</el-dropdown-item>
                <el-dropdown-item v-has="permissionUpStatus" @click.native="handleIsDisable(scope.row)">{{ scope.row.state == 0 ? '启用' : '禁用' }}</el-dropdown-item>
                <el-dropdown-item v-has="permissionDelete" v-if="scope.row.state == 0" @click.native="handleClickDelete(scope.row)">删除</el-dropdown-item>
                <el-dropdown-item v-has="permissionHeartbeat" @click.native="handleHeartbeatRecord(scope.row)">心跳记录</el-dropdown-item>
                <!-- <el-dropdown-item >心跳记录</el-dropdown-item> -->
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.pageNo"
        :page-sizes="[20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, jumper, next, pager, prev"
        :total="page.pageTotal"
      ></el-pagination>
    </el-row>

    <BeeperRecordTable ref="beeperRecordTable"></BeeperRecordTable>
    <BeeperHeartbeatTable ref="beeperHeartbeatTable"></BeeperHeartbeatTable>
  </div>
</template>

<script>
import * as message from '@/constants/message';
import BeeperRecordTable from '@/components/BeeperRecordTable';
import BeeperHeartbeatTable from '@/components/BeeperHeartbeatTable';
import EquipApi from '@/api/equip/equip';
export default {
  name: 'BeeperManagement',
  inject: ['reload'], //注入局部刷新
  data() {
    return {
      dataList: [],
      listLoading: true,
      filters: {
        beeperCode: '',
        beeperName: '',
      },
      page: {
        pageNo: 1,
        pageTotal: 0,
        pageSize: 20,
      },
      // 权限

      permissionAdd: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_ADD',
      permissionCheck: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_SEARCH',
      permissionEdit: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_EDIT',
      permissionDelete: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_DELETE',
      permissionBeeper: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_BEEPER',
      permissionHeartbeat: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_HEARTBEAT',
      permissionUpStatus: 'SYSTEM_MANAGER_EQUIP_BEEPER_LIST_STATE',
    };
  },
  components: {
    BeeperRecordTable,
    BeeperHeartbeatTable,
  },
  mounted() {
    this.init();
    // this.timer = setInterval(()=>{
    //   this.listData()
    // },3000)
    window.addEventListener('keydown', this.keyDown);
  },
  activated() {
    if (this.$route.meta.keepAlive) {
      this.listData();
    }
  },
  destroyed() {
    clearInterval(this.timer);
    window.removeEventListener('keydown', this.keyDown, false);
  },
  methods: {
    init() {
      let _this = this;
      _this.listLoading = true;
      _this.listData();
      _this.getTableHeight();
    },
    keyDown(e) {
      //如果是回车则执行查询方法
      if (e.keyCode == 13) {
        this.onSearch();
      }
    },

    getTableHeight() {
      this.$nextTick(function () {
        this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        // 浏览器尺寸变化响应事件
        window.onresize = () => {
          this.maxHeight = window.innerHeight - this.$refs.tableRow.$el.offsetTop - 50;
        };
      });
    },

    handleSizeChange(size) {
      let _this = this;
      _this.page.pageSize = size;
      _this.listData();
    },
    handleCurrentChange(val) {
      let _this = this;
      _this.page.pageNo = val;
      _this.listData();
    },
    formatState(cellValue) {
      if (cellValue == -1) {
        return '删除';
      } else if (cellValue == 0) {
        return '禁用';
      } else if (cellValue == 1) {
        return '启用';
      }
    },
    formatStates(cellValue) {
      if (cellValue == -1) {
        return 'danger';
      } else if (cellValue == 0) {
        return 'info';
      } else if (cellValue == 1) {
        return 'success';
      }
    },
    formatBeeperState(cellValue) {
      if (cellValue == 1) {
        return '待连接';
      } else if (cellValue == 2) {
        return '在线';
      } else if (cellValue == 3) {
        return '离线';
      }
    },
    formatBeeperStates(cellValue) {
      if (cellValue == 1) {
        return 'info';
      } else if (cellValue == 2) {
        return 'success';
      } else if (cellValue == 3) {
        return 'danger';
      }
    },
    formatRecordState(row, column, cellValue) {
      if (cellValue == 1) {
        return '待接单';
      } else if (cellValue == 2) {
        return '已接单';
      } else if (cellValue == 3) {
        return '已取消';
      } else if (cellValue == 4) {
        return '取消失败';
      } else if (cellValue == 20) {
        return '已完成';
      }
    },
    resetForm() {
      this.$refs.searchForm.resetFields();
      this.onSearch();
    },
    onSearch() {
      this.listLoading = true;
      this.page.pageNo = 1;
      this.listData();
    },
    listData() {
      let _this = this;
      var cond = {
        pageNo: _this.page.pageNo,
        pageSize: _this.page.pageSize,
        beeperCode: _this.filters.beeperCode,
        beeperName: _this.filters.beeperName,
      };
      // 发送请求
      EquipApi.listEquipBeeper(cond)
        .then((res) => {
          _this.listLoading = false;
          _this.dataList = res.data.list;
          _this.page.pageTotal = res.data.total;
        })
        .catch((err) => {
          _this.listLoading = false;
        });
    },
    handleBeeperRecord(val) {
      // 调用呼叫器记录
      this.$refs.beeperRecordTable.handleCreate(val.beeperId);
    },
    handleHeartbeatRecord(val) {
      // 调用心跳记录
      this.$refs.beeperHeartbeatTable.handleCreate(val.beeperId);
    },
    handleIsDisable(row) {
      let _this = this;
      let currentState = row.state == 0 ? 1 : 0;
      this.$confirm(currentState ? '是否启用？' : '是否禁用', message.TITLE_PROMPT, {}).then(() => {
        EquipApi.deleteEquipBeeper({
          id: row.beeperId,
          state: currentState,
        }).then((res) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    handleClickDelete(val) {
      // 删除
      this.$confirm('是否删除该呼吸器', message.TITLE_PROMPT, {}).then(() => {
        var cond = {
          id: val.beeperId,
          state: -1,
        };
        EquipApi.deleteEquipBeeper(cond).then((data) => {
          this.$Utils.notifySuccess();
          this.listData();
        });
      });
    },
    // handleRecordState(val){
    // let cond = {
    //   id:val.beeperId,
    //   recordState:2 //把待接单修改成已接单
    // }
    // EquipApi.changeState(cond).then(res=>{
    // console.log(console)
    // })
    // },
    handleClickDetail(row) {
      let _this = this;
      _this.$router.push({
        path: '/equip/beeperDetail',
        query: { id: row.beeperId },
      });
    },
    handleClickEditor(val) {
      //编辑 进入新增页通过id来查找数据
      let _this = this;
      _this.$router.push({
        path: '/equip/beeperEdit',
        query: { id: val.beeperId },
      });
    },
    handleClickAdd() {
      // 刷新
      let _this = this;
      _this.$router.push('/equip/beeperAdd');
    },
    handleHousehold() {
      this.$router.push('/equip/householdManagement');
    },
    handleBusiness() {
      this.$router.push('/equip/BusinessManagement');
    },
  },
};
</script>

<style lang="scss" src="@/assets/css/views/content.scss" scope></style>
